package com.fzshang.shopping.api.controller;


import cn.hutool.crypto.digest.DigestUtil;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Classification;
import com.fzshang.shopping.commen.bean.Journal;
import com.fzshang.shopping.commen.bean.User;
import com.fzshang.shopping.commen.bean.UserInfo;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import com.fzshang.shopping.commen.util.sms.SenSms;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.InetAddress;
import java.util.List;

/**
 * <p>
 * 后台用户 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-03
 */
@Controller
@RequestMapping("admin/userInfo")
public class UserInfoController extends BaseController {


    /**
     * @param
     * @param account
     * @param passWord
     * @return 登陆
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult login(String account, String passWord) {
        Subject subject = SecurityUtils.getSubject();
        String md5Hex1 = DigestUtil.md5Hex(passWord);
        UsernamePasswordToken token = new UsernamePasswordToken(account, md5Hex1);

        //进行验证，这里可以捕获异常，然后返回对应信息
        try {
            subject.login(token);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("account",account);
            UserInfo user = userInfoService.getOne(queryWrapper);
            if(user.getState()==1){
                return ApiResult.fail("该商家未审核,审核后在登陆");
            }else if(user.getState()==2){

                return ApiResult.ok();
            }else{
                return ApiResult.fail("该商家审核失败!");
            }
        }catch (Exception e){
            return ApiResult.fail("请核实账户或密码!");
        }
    }

    /**
     * 登出
     *
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();//取出当前验证主体
        if (subject != null) {
            subject.logout();//不为空，执行一次logout的操作，将session全部清空
        }
        return "admin/login";
    }


    /**
     * @param model
     * @return 今日必推
     */
    @RequestMapping(value = "must_push.html")
    public String must_push(Model model){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        model.addAttribute("userInfo",userInfoService.list(queryWrapper));
        return "admin/index/must_push";
    }

    /**
     * @param model
     * @return 没人推荐
     */
    @RequestMapping(value = "recommend.html")
    public String recommend(Model model){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        model.addAttribute("userInfo",userInfoService.list(queryWrapper));
        return "admin/index/recommend";
    }


    /**
     * @param model
     * @return 资料设置
     */
    @RequestMapping(value = "data_set.html")
    public String data_set(Model model){
        model.addAttribute("userInfo",getUserInfo());
        return "admin/data_set";
    }

    /**
     * @param userInfo
     * @return 资料修改
     */
    @RequestMapping(value = "updateUserInfo")
    @ResponseBody
    public ApiResult updateUserInfo(@RequestBody UserInfo userInfo){
        if(userInfoService.updateById(userInfo)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param model
     * @return 商家审核
     */
    @RequestMapping(value = "business.html")
    public String business(Model model){
        return "admin/business/business";
    }
    /**
     * @param model
     * @return 商家管理
     */
    @RequestMapping(value = "merchant_management.html")
    public String merchant_management(Model model){
        return "admin/business/merchant_management";
    }
    /**
     * @param voPage
     * @return 商家管理
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<UserInfo> selectMyPage(VoPage voPage, UserInfo userInfo){
        return userInfoService.selectMyPage(voPage,userInfo);
    }

    /**
     * @param voPage
     * @return 商家审核
     */
    @RequestMapping(value = "selectMyPage1",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<UserInfo> selectMyPage1(VoPage voPage, UserInfo userInfo){
        return userInfoService.selectMyPage1(voPage,userInfo);
    }

    /**
     * @param id
     * @return 通过
     */
    @RequestMapping(value = "success")
    @ResponseBody
    public ApiResult success(Long id) throws ClientException {
       UserInfo userInfo = userInfoService.getById(id);
       userInfo.setState(2);
       if(userInfoService.updateById(userInfo)){
           QueryWrapper queryWrapper = new QueryWrapper();
           queryWrapper.eq("openid",userInfo.getOpenid());
           User user = userService.getOne(queryWrapper);
           user.setUserIdentity(2);
           if(userService.updateById(user)){
               //发送短信通知
               SenSms.success(userInfo.getPhone(),userInfo.getAccount(),userInfo.getPassWord());
                return ApiResult.ok();
           }
       }
        return ApiResult.fail();
    }
    /**
     * @param id
     * @return 失败
     */
    @RequestMapping(value = "error")
    @ResponseBody
    public ApiResult error(Long id,String content) throws ClientException {
        UserInfo userInfo = userInfoService.getById(id);
        if(userInfoService.removeById(id)){
            //审核失败 原因
            SenSms.error(userInfo.getPhone(),content);
          return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    //商家收入
    @RequestMapping(value = "selectMoney",method = RequestMethod.GET)
    @ResponseBody
    public List selectMoney() throws Exception {
        return userInfoService.selectMoney();
    }
    //销售收入
    @RequestMapping(value = "selectMoneySale",method = RequestMethod.GET)
    @ResponseBody
    public List selectMoneySale() throws Exception {
        return userInfoService.selectMoneySale();
    }

    /**
     * @return  审核失败
     */
    @RequestMapping(value = "error.html")
    public String error(Model model,Long id){
        model.addAttribute("id",id);
        return "admin/business/error";
    }

}
